package com.hhkj.study.admin.article.service.biz.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.google.common.collect.Lists;
import com.hhkj.study.admin.article.entity.ArticleBanner;
import com.hhkj.study.admin.article.enums.BannerEnum;
import com.hhkj.study.admin.article.model.podo.ArticleBannerDo;
import com.hhkj.study.admin.article.model.podo.ArticleBannerPo;
import com.hhkj.study.admin.article.model.req.ArticleBannerReq;
import com.hhkj.study.admin.article.model.res.ArticleBannerRes;
import com.hhkj.study.admin.article.service.base.ArticleBannerService;
import com.hhkj.study.admin.article.service.biz.ArticleBannerBizService;
import com.hhkj.study.admin.article.service.extend.ArticleBannerExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.hhkj.study.common.handler.CommonHandler;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.exception.GlobalException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* ArticleBannerBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class ArticleBannerBizServiceImpl extends BaseBizServiceImpl<ArticleBanner, ArticleBannerReq, ArticleBannerRes, ArticleBannerDo, ArticleBannerPo, ArticleBannerService, ArticleBannerExtendService> implements ArticleBannerBizService {

    @Resource
    private GlobalCache globalCache;

    @Override
    public boolean updatePublishFlag(ArticleBannerReq req) {
        ArticleBanner articleBanner = this.getExtendService().getByIdNotNull(req.getId());
        if (articleBanner.getPublishedFlag().equals(req.getPublishedFlag())) {
            throw new GlobalException(SysConst.Flag.IS.getFlag().equals(req.getPublishedFlag()) ?
                    "已上架" : "已下架");
        }
        LambdaUpdateWrapper<ArticleBanner> bannerUpdate = new LambdaUpdateWrapper<>();
        bannerUpdate.eq(ArticleBanner::getId, req.getId())
                .set(ArticleBanner::getPublishedFlag, req.getPublishedFlag());
        return this.getService().update(bannerUpdate);
    }

    @Override
    protected void afterGetById(ArticleBannerRes articleBannerRes) {
        if (ObjectUtil.isNull(articleBannerRes)) {
            return;
        }
        suppleBanner(Lists.newArrayList(articleBannerRes));
    }

    @Override
    protected void afterPage(List<ArticleBannerRes> articleBannerResList) {
        suppleBanner(articleBannerResList);
    }

    private void suppleBanner(List<ArticleBannerRes> articleBannerResList) {
        Map<Long, String> dictMap = globalCache.cacheDictMap(Constants.REGION_COURSE_CODE_LIST);
        articleBannerResList.forEach(articleBannerRes -> {
            articleBannerRes.setCatalogName(BannerEnum.getDescByCode(articleBannerRes.getCatalogCode()));
            articleBannerRes.setCourseTypeName(CommonHandler.joinStr(dictMap, articleBannerRes.getCourseTypeList(), Constants.VERTICAL_LINE));
            articleBannerRes.setCourseTypeMap(CommonHandler.getMapByDict(dictMap, articleBannerRes.getCourseTypeList()));
            articleBannerRes.setProvinceName(CommonHandler.joinStr(dictMap, articleBannerRes.getProvinceIdList(), Constants.VERTICAL_LINE));
            articleBannerRes.setProvinceMap(CommonHandler.getMapByDict(dictMap, articleBannerRes.getProvinceIdList()));
        });
    }
}